/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.huaxindata.im.main.chat.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.huaxindata.im.R;
import com.huaxindata.im.model.emoji.HxEmotion;

import java.util.ArrayList;
import java.util.List;

//表情栏中ViewPager的适配器
public class EmojiPagerAdapter extends PagerAdapter implements AdapterView.OnItemClickListener{

	private List<HxEmotion> emojiList;
	private LayoutInflater inflater;
	private Context context;
	private OnEmojiItemClickListenner emojiItemClickListenner;

	public EmojiPagerAdapter(Context context, List<HxEmotion> emojiList, OnEmojiItemClickListenner emojiItemClickListenner) {
		this.context=context;
		this.emojiList=emojiList;
		inflater=LayoutInflater.from(context);
		this.emojiItemClickListenner=emojiItemClickListenner;
	}

	/**
	 * @return :返回表情的页数
	 */
	@Override
	public int getCount() {
		int count;
		if (emojiList == null) {
			count=0;
		} else {
			int size=emojiList.size();
			if (size % 27 == 0) {
				count=size / 27;
			} else {
				count=size/27+1;
			}
		}
		return count;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = inflater.inflate(R.layout.expression_gridview, container, false);
		GridView gridView = (GridView) view.findViewById(R.id.gridview);
		//获取第一页的emoji数
		List<HxEmotion> emojiPageList=new ArrayList<>(27);
		int total=emojiList.size();
		for (int i=position*27;i<27*(position+1);i++) {
			if (i < total) {
				emojiPageList.add(emojiList.get(i));
			}
		}

		EmojiAdapter emojiGridAdapter = new EmojiAdapter(context, emojiPageList);
		gridView.setAdapter(emojiGridAdapter);
		gridView.setOnItemClickListener(this);
		container.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		emojiItemClickListenner.onEmojiItemClick(position);
	}
	//ViewPager中的表情点击之后的回调接口
	public interface OnEmojiItemClickListenner{
		void onEmojiItemClick(int position);
	}
}
